<?php


class ErrorController extends Silk_Controller_Action {

    public function errorAction(){

// 	logdebug("ErrorController.errorAction: Called. Params=" . logobject($this->_getAllParams()));
	$error_handler = $this->_getParam('error_handler');
	$exception = $error_handler->exception;

	$code      = $exception->getCode();

	logdebug('ErrorController.errorAction: ' . get_class($exception));

	if ($exception instanceof FP_Acl_Exception){

	    if ($code == FP_Acl_Exception::ACCESS_DENIED){

		$this->_forward('deny');

	    } else if ($code == FP_Acl_Exception::AUTH_REQUIRED){

		$this->_forward('authenticate');
	    }

	} else if ($exception instanceof FP_Route_Exception){

	    logdebug('ErrorController.errorAction: ' . $exception->getMessage());

	    if ($code == FP_Route_Exception::NO_ROUTE){
		$this->_forward('noroute');
	    } else if ($code == FP_Route_Exception::NO_RESOURCE){
		$this->_forward('noresource');
	    }

	} else {

	    logexception($exception);

	    $this->_forward('unhandled', 'error', null, array('exception' => $exception));

	}
    }

    public function norouteAction(){
	$this->_response->setHeader('HTTP/1.0', '404 Not Found');
    }

    public function noresourceAction(){
	$this->_response->setHeader('HTTP/1.0', '404 Not Found');
    }

    public function unhandledAction(){
	$this->_response->setHeader('HTTP/1.1', '500 Internal Server Error');

	$this->view->exception  = $this->_getParam('exception');
	$this->view->show_trace = Silk::getConfig()->error->show_trace;
    }

    public function authenticateAction(){

	logdebug('ErrorController.authenticate', 'Called');

	$this->_response->setHeader('HTTP/1.1', '401 Unauthorized');

	$session = Silk::getSession();

	$session->acl_request_uri = $this->_request->getRequestUri();

	$this->gotoRoute('login');

    }

    public function denyAction(){
	$this->_response->setHeader('HTTP/1.1', '403 Forbidden');

    }
}

?>